package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.StoreInfoResult.StoreInfos;

import java.util.ArrayList;

public class StoreInfoAdapter extends BaseAdapter {
    ArrayList<StoreInfos> datas;
    Context context;
    LayoutInflater inflater;

    public StoreInfoAdapter(ArrayList<StoreInfos> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > position ? datas.get(position)
                .getImg_url() : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            Holder holder = new Holder();
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.store_info_item, null);
            }
            holder.img = (ImageView) convertView
                    .findViewById(R.id.store_item_img);
            holder.img.setScaleType(ScaleType.FIT_XY);
            if (position < datas.size() && datas.get(position) != null
                    && datas.get(position).getImg_url() != null
                    && !"".equals(datas.get(position).getImg_url())) {
//				MyApplication.loadImage(context, datas.get(position)
//						.getImg_url(), holder.img);
                MyApplication.loadImageByPicasso(context, datas.get(position)
                        .getImg_url(), null, holder.img, 0, 0, 1);
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class Holder {
        ImageView img;
    }

}
